﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JihankiLib
{
    /// <summary>
    /// お金パーサーインターフェイス
    /// </summary>
    public interface IMoneyParser
    {
        MoneySet Parse(int money);
    }

    /// <summary>
    /// お金パーサーデフォルト実装
    /// </summary>
    public class DefaultMoneyParser : IMoneyParser
    {
        public MoneySet Parse(int money)
        {
            var unit = new[] {
                MoneyType.BILL_10000,
                MoneyType.BILL_5000,
                MoneyType.BILL_1000,
                MoneyType.COIN_500,
                MoneyType.COIN_100,
                MoneyType.COIN_50,
                MoneyType.COIN_10
            };
            int rest = money;
            var mset = new MoneySet();
            for(var i=0;i<unit.Length;i++)
            {
                int amount = 0;
                if(parse(rest, MoneyUnitValueAttribute.GetUnitValue(unit[i]), ref amount, ref rest))
                    mset.Add(new Money(unit[i], amount));
            }

            return mset;
        }

        private bool parse(int money, int unit, ref int amount, ref int rest)
        {
            if (money < unit)
                return false;
            amount = money / unit;
            rest = money - unit * amount;
            return true;
        }
    }

    /// <summary>
    /// お金パーサー
    /// </summary>
    public class MoneyParser
    {
        private static IMoneyParser parser = null;
        public static IMoneyParser Current
        {
            get {
                if (parser == null)
                    parser = new DefaultMoneyParser();
                return parser;
            }
            set
            {
                parser = value;
            }
        }
    }
}
